﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.IO;

namespace HashMe
{
    public static class EncryptTypes
    {

        public static String GetEncrypt(String EncryptType, String pTextToEncrypt,String pPassphrase)
        {

            Object[] parameter = { pTextToEncrypt,pPassphrase };
            MethodInfo mi = typeof(EncryptTypes).GetMethod("Get" + EncryptType + "Encrypt");
            String result = mi.Invoke(null, parameter).ToString();
            return result;
        }

        public static String GetRijndaelEncrypt(String pTextToEncrypt, String pPassphrase)
        {
            byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(pTextToEncrypt);
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(pPassphrase, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            byte[] encryptedData = EncryptRijndaelString(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
            return Convert.ToBase64String(encryptedData);
        }

        private static byte[] EncryptRijndaelString(byte[] clearText, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            Rijndael alg = RijndaelManaged.Create();
            alg.Key = Key;
            alg.IV = IV;
            CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(clearText, 0, clearText.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();
            return encryptedData;
        }

    }
}
